<?php
apf_require_controller("Abstract");
apf_require_class("Const_BaseInfo");
class Portal_Home_SearchController extends AbstractController {

    public function handle_request() {
        $pam=$this->request->get_parameters();
        $rs = array();
        if($pam["ajax"]==1)
        {
            $rs = $this->get_data_bll()->search(array("kw" =>urldecode($pam['kw']), "cs" => $pam['cs'], "sy" => $pam['sy'], "bm" => $pam['bm']),'',10,10);
            if(empty($rs["rs"]))
            exit(0);

            foreach($rs["rs"] as $k=>$v)
            {
                foreach($v as $k2=>$r)
                {
                    if(empty($r))
                    $rs["rs"][$k][$k2]="&nbsp;";
                }
            }
            //echo(json_encode($rs));
            //exit();
        }
        $codelist = array();
        if($pam['kw']){
            $rs = $this->get_data_bll()->search(array("kw" =>$pam['kw'], "cs" => $pam['cs'], "sy" => $pam['sy'], "bm" => $pam['bm']),'',10,10);
            foreach((array)$rs["rs"] as $k=>$v)
            {
                $codelist[] = $v['user_code'];
                foreach($v as $k2=>$r)
                {
                    if(empty($r))
                    $rs["rs"][$k][$k2]="&nbsp;";
                }
            }
        }
        $user_game_list = array();
        if(!empty($codelist)){
            $user_game_list = $this->get_game_status($codelist);
            $this->request->set_attribute("user_game_list",json_encode($user_game_list));
        }
        $uid = $this->get_user_id();
        $projects = $this->get_data_bll2()->get_sort_projects($uid);
        $id = $this->request->get_parameter("id");
        if (!empty($id) && key_exists($id, $projects)){
            $this->get_data_bll2()->inc_view_count($uid, $id);
            $this->response->redirect($projects[$id]['url']);
        }
        if(!$rs){
            $cities = $this->get_property_bll()->get_city_list();
            $cities = $this->_build_key_value($cities,'city_id','city_name');
            $departments = $this->get_property_bll()->get_function_list();
            $departments = $this->_build_key_value($departments,'function_id','function_name');
            $divisions = $this->get_property_bll()->get_business_list();
            $divisions = $this->_build_key_value($divisions,'bu_id','bu_name');
        }
        $page_rows = $pam["kw"]=='techmgr' ? 100 : 6;
        $this->request->set_attribute("page_rows",$page_rows);
        $this->request->set_attribute("kw",$pam["kw"]);
        $this->request->set_attribute("param",$pam);
        $this->request->set_attribute("rs",$rs["rs"]);
        $this->request->set_attribute("sy",$rs["sy"]);
        $this->request->set_attribute("cs",$rs["cs"]);
        $this->request->set_attribute("bm",$rs["bm"]);
        $this->request->set_attribute("cities",$cities);
        $this->request->set_attribute("divisions",$divisions);
        $this->request->set_attribute("departments",$departments);
        $this->request->set_attribute('projects', $projects);
        return "Portal_Home_Search";
    }
    private function get_game_status($codelist){
        if(empty($codelist)){
            return array();
        }
        $codestr = implode('-',$codelist);
        $host = 'http://point.corp.anjuke.com/lianliankan/?op=api&p='.$codestr;
        $str= file_get_contents($host);
        $last = json_decode($str);
        if(empty($last->result)){
            return array();
        }
        return $last->response;

    }
    private function get_data_bll(){
        if (!$this->data_bll || !$this->data_bll instanceof Bll_Portal_HomeData){
            apf_require_class("Bll_Portal_HomeData");
            $this->data_bll = new Bll_Portal_HomeData();
        }
        return $this->data_bll;
    }
    private function get_data_bll2(){
        if (!$this->data_bll || !$this->data_bll instanceof Bll_Portal_HomeProjectView){
            apf_require_class("Bll_Portal_HomeProjectView");
            $this->data_bll = new Bll_Portal_HomeProjectView();
        }
        return $this->data_bll;
    }
    /**
     * 参数
     * @return Bll_Hr_Property
     */
    private function get_property_bll(){
        if (!$this->property_bll || !$this->property_bll instanceof Bll_Hr_Property){
            apf_require_class("Bll_Hr_Property");
            $this->property_bll = new Bll_Hr_Property();
        }
        return $this->property_bll;
    }
    /**
     * 索引值
     * @param unknown_type $data
     * @param unknown_type $key
     * @param unknown_type $value
     */
    private function _build_key_value($data,$key,$value){
        $result = array();
        foreach($data as $one){
            $result[$one[$key]] = $one[$value];
        }
        unset($data);
        return $result;
    }
}
